package com.chlitina.store.modules.inventory.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.utils.StoreProductUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.base.service.CustomerService;
import com.chlitina.store.modules.base.service.StoreCareService;
import com.chlitina.store.modules.base.service.StoreProductService;
import com.chlitina.store.modules.base.service.StoreSelfProductService;
import com.chlitina.store.modules.cash.web.Types;
import com.chlitina.store.modules.employee.dao.EmployeeDao;
import com.chlitina.store.modules.employee.entity.Employee;
import com.chlitina.store.modules.expert.service.ProductService;
import com.chlitina.store.modules.inventory.model.InventoryQuery;
import com.chlitina.store.modules.inventory.service.InventoryService;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * Inventory Controller
 * 
 * @author Songsong.xu
 * @version 2013-11-05
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/inventory")
public class InventoryController extends BaseController {
	@Autowired
	private ProductService productService;
	@Autowired
	private StoreCareService storeCareService;
	@Autowired
	private StoreProductService storeProductService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private InventoryService inventoryService;
	@Autowired
	private StoreSelfProductService storeSelfProductService;
	public static int DEFAULT_PAGE_SIZE = 5;
	

	@RequestMapping(value = { "query" })
	public String inventoryQuery(InventoryQuery inventoryQuery,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<StoreProduct> page = new Page<StoreProduct>(request, response);
		String includingZero = request.getParameter("includingZero");
		if ("0".equals(includingZero)) {
			inventoryQuery.setIncludingZero(true);
		} else if ("1".equals(includingZero)) {
			inventoryQuery.setIncludingZero(false);
		}
		inventoryQuery = storeProductService.findStoreProducts(inventoryQuery);
		model.addAttribute("inventoryQuery", inventoryQuery);
		page.updatePage(inventoryQuery.getStoreProducts());
		model.addAttribute("page", page);
		return "modules/inventory/inventoryQuery";
	}

	public void updateEmployee(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Object attribute = session.getAttribute("employees");
		if (attribute == null) {
			session.setAttribute("employees",
					employeeDao.findListByStore(StoreUtils.getStore().getId(),Employee.STATUS_WORKING));
		} else if (attribute instanceof List<?>) {
			List<?> a = (List<?>) attribute;
			if (a.size() == 0) {
				session.setAttribute("employees", employeeDao
						.findListByStore(StoreUtils.getStore().getId(),Employee.STATUS_WORKING));
			}
		}
	}


	@RequestMapping(value = { "check" })
	public String inventoryCheck() {
		return "modules/inventory/inventoryCheck";
	}
}
